import {
  validateCityName,
  validateAddress,
  validateTelphone1,
  validateDeviceId1
} from '@/utils/rules'

// const validateFunc = (rule, value, callback) => {
//   if (value.length == 0) {
//     callback(new Error("至少选择一个功能"));
//   } else {
//     callback();
//   }
// };
export const addForm = {
  cityId: '',
  name: '',
  alias: '',
  address: '',
  contactName: '',
  contactPhone: '',
  lng: null,
  lat: null,
  transformerStructure: 'false',
  powerPeriod: 1,
  func: [],
  mainTotal: null,
  mainCapacity: null,
  tradeId: '',
  businessId: '',
  // 平台采集
  sysFunc: '',
  // 企业渠道
  channelId:""

}

export const addFormRules = {
  name: [
    {
      required: true,
      validator: validateCityName('企业名称', '企业名称只能输入中文、字母'),
      trigger: 'blur'
    }
  ],
  alias: [
    {
      required: true,
      validator: validateCityName('企业别名', '企业别名只能输入中文、字母'),
      trigger: 'blur'
    }
  ],
  cityId: [
    {
      required: true,
      message: '请选择所属城市',
      trigger: 'change'
    }
  ],
  businessId: [
    {
      required: true,
      message: '请选择所属行业',
      trigger: 'change'
    }
  ],
  transformerStructure: [
    {
      required: true,
      trigger: 'change'
    }
  ],
  address: [
    {
      required: true,
      validator: validateAddress(
        '地址',
        '地址只能输入中文、字母、数字、下划线和中划线'
      ),
      trigger: 'blur'
    }
  ],
  contactName: [
    {
      required: true,
      validator: validateAddress(
        '联系人',
        '联系人只能输入中文、字母、数字、下划线和中划线'
      ),
      trigger: 'blur'
    }
  ],
  contactPhone: [
    {
      required: true,
      validator: validateTelphone1,
      trigger: 'blur'
    }
  ],
  mainTotal: [
    {
      required: true,
      validator: validateDeviceId1('主变容量', '主变容量别名只能输入正整数'),
      trigger: 'blur'
    }
  ],
  mainCapacity: [
    {
      required: true,
      validator: validateDeviceId1('主变台数', '主变台数只能输入正整数'),
      trigger: 'blur'
    }
  ],
  // func: [{
  //   required: true,
  //   validator: validateFunc,
  //   trigger: "change"
  // }],
  powerPeriod: [
    {
      required: true,
      message: '请选择电量计算周期',
      trigger: 'change'
    }
  ],
  tradeId: [
    {
      required: true,
      trigger: 'change',
      message: '请选择行业'
    }
  ]
}

export const buildFunc = (params, arr) => {
  if (arr.indexOf('thermalImager') > -1) {
    params['thermalImager'] = true
  } else {
    params['thermalImager'] = false
  }

  if (arr.indexOf('humitureState') > -1) {
    params['humitureState'] = true
  } else {
    params['humitureState'] = false
  }
  return params
}

export const funcBuild = params => {
  let arr = []
  if (!!params.thermalImager) arr.push('thermalImager')
  if (!!params.humitureState) arr.push('humitureState')
  return arr
}

export const transStringToBoolean = str => {
  if (str === 'true') return true
  else return false
}

export const booleanToString = bool => {
  if (bool) return 'true'
  else return 'false'
}

export const disposalData = (group=[], device={}) => {
  const result = []
  group.forEach(t => {
    t.children = device[t.id].map(y=>{
      y['isDisabled'] =  false
      return y
    })
    result.push(t)
  })
  return result;
}
